package com.xasz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xasz.dto.InputDto;
import com.xasz.entity.Activity;
import com.xasz.entity.Candidate;
import com.xasz.entity.Jury;
import com.xasz.entity.User;
import com.xasz.service.IActivityService;
import com.xasz.service.impl.*;
import com.xasz.util.JsonResult;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cai
 * @since 2022-07-28
 */
@AllArgsConstructor
@RestController
@RequestMapping("activity")
public class ActivityController extends BaseController{
    private IActivityService iActivityService;
    private ActivityServiceImpl activityService;
    private JuryServiceImpl juryService;
    /**
     * 创建新活动
     */
    @PostMapping
    public JsonResult<String> inputActivity(@RequestBody InputDto inputDto, HttpSession session){
        activityService.creatActivity(inputDto,getUidFromSession(session));
        return new JsonResult<>(OK);
    }
    /**
     * 删除活动
     */
    @DeleteMapping("{Aid}")
    public JsonResult<Void> DeleteActivity(@PathVariable Integer Aid,HttpSession session){
        activityService.detectActivity(Aid,getUidFromSession(session),getUsernameFromSession(session));
        return new JsonResult<>(OK);
    }
    /**
     * 增加查询用户创建的活动信息
     */
    @GetMapping
    public JsonResult<List<Activity>> getActivity(HttpSession session){
        QueryWrapper<Activity> wrapper=new QueryWrapper<Activity>().eq("owner",getUidFromSession(session));
        return new JsonResult<>(OK,iActivityService.list(wrapper));
    }
    //通过uid查询用户参加的活动
    @GetMapping("{Uid}")
    public JsonResult<List<Activity>> getActivityByUid(@PathVariable Integer Uid,HttpSession session){
        return null;
    }

    /**
     * 修改活动信息
     */
    @PutMapping
    public JsonResult<Void> updateActivity(@RequestBody Activity activity,HttpSession session){
        activityService.upDataActivity(activity,getUidFromSession(session));
        return new JsonResult<>(OK);
    }
    /**
     * 用户登录后可查询自己参加的所有活动
     */
    @GetMapping("/user")
    public JsonResult<List<String>> getUserActivity(HttpSession session){
        QueryWrapper<Jury> wrapper=new QueryWrapper<Jury>().eq("name",getUsernameFromSession(session));
        List<String> activities = new ArrayList<>();
        for (Jury jury:juryService.list(wrapper)){
            QueryWrapper<Activity> queryWrapper=new QueryWrapper<Activity>().eq("id",jury.getActivityId());
            activities.add(activityService.getOne(queryWrapper).getName());
        }
        return new JsonResult<>(OK,activities);
    }




    //超级管理员专用查询所有活动
    @GetMapping("/super")
    public JsonResult<List<Activity>> getAllActivity(){
        return new JsonResult<>(OK,iActivityService.list());
    }

}
